package com.demon.mutehelper

import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.huawei.hms.kit.awareness.Awareness
import com.huawei.hms.kit.awareness.barrier.BarrierStatus
import com.huawei.hms.kit.awareness.barrier.BarrierUpdateRequest
import com.huawei.hms.kit.awareness.barrier.TimeBarrier
import java.util.Calendar
import java.util.TimeZone

class TimeAbilityActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    init()
    initBarrier()
  }

  @SuppressLint("MissingPermission")
  private fun init() {
    Awareness.getCaptureClient(this).timeCategories.addOnSuccessListener {
      val timeCategories = it.timeCategories
      val timeInfo = timeCategories.timeCategories
    }.addOnFailureListener {
      Log.e(TAG, "get Time Categories failed", it)
    }
  }

  private fun initBarrier() {
    val oneHourMilliSecond = 60 * 60 * 1000L
    val calendar = Calendar.getInstance()

//    val periodOfDayBarrier = TimeBarrier.duringPeriodOfDay(
//      TimeZone.getDefault(),
//      13 * oneHourMilliSecond,
//      14 * oneHourMilliSecond
//    )

    val periodOfDayBarrier =
      TimeBarrier.duringTimePeriod(calendar.timeInMillis + 1000, calendar.timeInMillis + 1000 * 60)

    val BARRIER_RECEIVER_ACTION = application.packageName + "TIME_BARRIER_RECEIVER_ACTION"
    val intent = Intent(BARRIER_RECEIVER_ACTION)
    val pendingIntent =
      PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    val barrierReceiver = TimeBarrierReceiver()
    registerReceiver(barrierReceiver, IntentFilter(BARRIER_RECEIVER_ACTION))

    val timeBarrierLabel = "period of day barrier"
    // 定义更新围栏的请求
    val builder = BarrierUpdateRequest.Builder()
    val request = builder.addBarrier(timeBarrierLabel, periodOfDayBarrier, pendingIntent).build()
    Awareness.getBarrierClient(this).updateBarriers(request)
      // 执行成功的回调监听
      .addOnSuccessListener {
        Toast.makeText(applicationContext, "add barrier success", Toast.LENGTH_SHORT).show()
      }
      // 执行失败的回调监听
      .addOnFailureListener { e ->
        Toast.makeText(applicationContext, "add barrier failed", Toast.LENGTH_SHORT).show()
        Log.e(TAG, "add barrier failed", e)
      }
  }

  // 定义广播接收器，用于监听Barrier事件
  internal inner class TimeBarrierReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
      val barrierStatus = BarrierStatus.extract(intent)
      val label = barrierStatus.barrierLabel
      when (barrierStatus.presentStatus) {
        BarrierStatus.TRUE -> {
          Log.i(TAG, "$label status:true")
          deleteBarrier()
        }
        BarrierStatus.FALSE -> Log.i(TAG, "$label status:false")
        BarrierStatus.UNKNOWN -> Log.i(TAG, "$label status:unknown")
      }
    }
  }

  override fun onDestroy() {
    super.onDestroy()
  }

  private fun deleteBarrier() {
    val timeBarrierLabel = "period of day barrier"
// 定义更新围栏的请求
    val builder = BarrierUpdateRequest.Builder()
    val request = builder.deleteBarrier(timeBarrierLabel).build()
    Awareness.getBarrierClient(this).updateBarriers(request)
      // 执行成功的回调监听
      .addOnSuccessListener {
        Toast.makeText(applicationContext, "delete barrier success", Toast.LENGTH_SHORT).show()
      }
      // 执行失败的回调监听
      .addOnFailureListener { e ->
        Toast.makeText(applicationContext, "delete barrier failed", Toast.LENGTH_SHORT).show()
        Log.e(TAG, "delete barrier failed", e)
      }
  }

  private companion object {
    private const val TAG = "TimeAbilityActivity"
  }
}